package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.PaperZone;
import com.think.teaching.service.impl.PaperZoneServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 试卷分区 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "试卷分区理接口")
public class PaperZoneController {
	
	private final PaperZoneServiceImpl paperZoneService;

	public PaperZoneController(PaperZoneServiceImpl paperZoneService) {
		this.paperZoneService = paperZoneService;
	}

	@ApiOperation("添加新的试题试卷分区")
	@PostMapping("admin/paperZone")
	public Result AddNewPaperZone(@RequestBody()PaperZone data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			PaperZone paperZone = paperZoneService.AddPaperZone(data);
			if (paperZone != null) {
				return Result.success(paperZone);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("更新试题试卷分区")
	@RequestMapping(value = "admin/paperZone",method = RequestMethod.PUT)
	public Result UpdatePaperZone(@RequestBody()PaperZone data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			PaperZone paperZone = paperZoneService.UpdatePaperZone(data);
			if (paperZone != null) {
				return Result.success(paperZone);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("删除试题试卷分区")
	@RequestMapping(value = "admin/paperZone/{id}",method = RequestMethod.DELETE)
	public Result DeletePaperZone(@PathVariable("id")Integer id) {
		PaperZone paperZone = paperZoneService.getById(id);
		if (paperZone == null) {
			return Result.failure(ResultCode.PAPER_ZONE_NOT_EXIST);
		} else {
			if (paperZoneService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("管理员获取试题试卷分区详情")
	@RequestMapping(value = "admin/paperZone/{id}",method = RequestMethod.GET)
	public Result GetPaperZone(@PathVariable("id")Integer id) {
		PaperZone paperZone = paperZoneService.getById(id);
		if (paperZone == null) {
			return Result.failure(ResultCode.PAPER_ZONE_NOT_EXIST);
		} else {
			return Result.success(paperZone);
		}
	}
	
	@ApiOperation("管理员修改试卷分区状态")
	@RequestMapping(value = "admin/paperZone/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdatePaperZoneState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		PaperZone paperZone = paperZoneService.getById(id);
		if (paperZone == null) {
			return Result.failure(ResultCode.PAPER_ZONE_NOT_EXIST);
		} else {
			try {
				paperZone = paperZoneService.SetPaperZoneState(id, state);
				return Result.success(paperZone);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
}

